package cn.jsj.onclock.controller;

import cn.jsj.onclock.interceptor.swich.PassToken;
import cn.jsj.onclock.pojo.GroupEnt;
import cn.jsj.onclock.pojo.UserEnt;
import cn.jsj.onclock.pojo.UserGroupInfo;
import cn.jsj.onclock.pojo.identity.UserIdentityEnum;
import cn.jsj.onclock.result.Result;
import cn.jsj.onclock.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.jsj.onclock.interceptor.RequestThreadContext.requesterIdentity;
import static cn.jsj.onclock.interceptor.RequestThreadContext.requesterUserId;

/**
 * @author 卢斌
 */
@Controller
@CrossOrigin
@ResponseBody
@RequestMapping(value = "/group")
public class GroupController {


    private final GroupService groupService;

    @Autowired
    public GroupController(GroupService groupService) {
        this.groupService = groupService;
    }

    /**
     * 创建一个群组
     */
    @PostMapping(value = "/group")
    public Result<Long> creat(@RequestBody GroupEnt groupEnt) {
        Long userId = requesterUserId();
        UserIdentityEnum identity = requesterIdentity();
        Long newId = groupService.creatGroup(groupEnt.getGroupName(), userId, identity);
        return Result.success(newId);
    }


    /**
     * 获取用户加入的群组列表，分页显示
     */
    @GetMapping(value = "/user_in_group", params = {"pageNum", "pageSize"})
    public Result<UserGroupInfo> useAbout(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Long userId = requesterUserId();
        List<UserGroupInfo> storage = groupService.listUserGroupInformationAboutUser(userId, pageNum, pageSize);
        return Result.success(storage);
    }


    /**
     * 获取某一群组内的成员信息
     */
    @GetMapping(value = "/users", params = {"groupId", "pageNum", "pageSize"})
    public Result<UserGroupInfo> userList(@RequestParam("groupId") Long groupId, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Long userId = requesterUserId();
        List<UserGroupInfo> storage = groupService.listUserInfoInGroup(userId, groupId, pageNum, pageSize);
        return Result.success(storage);
    }


    /**
     * 获取某一群组内的成员信息
     */
    @GetMapping(value = "/all_users", params = {"groupId"})
    public Result<UserGroupInfo> userListWithoutPage(@RequestParam("groupId") Long groupId) {
        Long userId = requesterUserId();
        List<UserGroupInfo> storage = groupService.listUserInfoInGroupWithoutPage(userId, groupId);
        return Result.success(storage);
    }


    /**
     * 修改群组名称
     */
    @PutMapping(value = "/group")
    public Result<String> resetGroupName(@RequestBody GroupEnt groupEnt) {
        Long userId = requesterUserId();
        String newName = groupService.updateGroupName(userId, groupEnt.getId(), groupEnt.getGroupName());
        return Result.success(newName);
    }


    /**
     * 删除一个数据集
     */
    @DeleteMapping(value = "/group")
    public Result<Long> deleteGroup(@RequestBody GroupEnt groupEnt) {
        Long userId = requesterUserId();
        Long deleteId = groupService.deleteOneGroup(userId, groupEnt.getId());
        return Result.success(deleteId);
    }


    /**
     * 模糊查询群组列表
     */
    @PassToken
    @GetMapping(value = "/prefix_name", params = {"prefix", "pageNum", "pageSize"})
    public Result<GroupEnt> likeName(@RequestParam("prefix") String prefix, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        List<GroupEnt> storage = groupService.findNameGroup(prefix, pageNum, pageSize);
        return Result.success(storage);
    }


    /**
     * 获取用户在某群组中的成员信息（一般用于检查用户是否在该群组中）
     */
    @GetMapping(value = "/user_in_group", params = {"groupId"})
    public Result<UserGroupInfo> userInGroup(@RequestParam("groupId") Long groupId) {
        Long userId = requesterUserId();
        UserGroupInfo userGroupInfo = groupService.getUserGroupInfo(userId, groupId);
        return Result.success(userGroupInfo);
    }


    /**
     * 用户修改群审批方式
     */
    @PutMapping(value = "/group_request_way")
    public Result<Object> updateRequestAuthWay(@RequestBody GroupEnt groupEnt) {
        Long userId = requesterUserId();
        groupService.updateGroupAuthWay(userId, groupEnt.getId(), groupEnt.getJoinAuth());
        return Result.success();
    }


    /**
     * 查询符合前缀规则的用户已经加入的群组
     */
    @GetMapping(value = "/prefix_about_user", params = {"prefix", "pageNum", "pageSize"})
    public Result<UserGroupInfo> listLikeNameAndAboutUser(@RequestParam("prefix") String prefix, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Long userId = requesterUserId();
        List<UserGroupInfo> storage = groupService.listGroupLikePrefixNameAboutUser(userId, prefix, pageNum, pageSize);
        return Result.success(storage);
    }


    /**
     * 查询群友的个人信息
     */
    @GetMapping(value = "/friend_info", params = {"friendId", "groupId"})
    public Result<UserEnt> getGroupFriendInfo(@RequestParam("friendId") Long friendId, @RequestParam("groupId") Long groupId) {
        Long userId = requesterUserId();
        UserEnt userInfo = groupService.getOneGroupFriendInfo(userId, friendId, groupId);
        return Result.success(userInfo);
    }


}
